package com.zrwl.poscloud.xo.service.impl;
import com.zrwl.poscloud.base.entity.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.EmployeeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import com.zrwl.poscloud.xo.mapper.StudentScoreSubjectDetailMapper;
import com.zrwl.poscloud.xo.vo.StudentScoreSubjectDetailVO;

import javax.annotation.Resource;

/**
 * 成绩明细 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-10
 */
@Service
public class StudentScoreSubjectDetailServiceImpl extends SuperServiceImpl<StudentScoreSubjectDetailMapper,StudentScoreSubjectDetail> implements StudentScoreSubjectDetailService {

    @Autowired
    private StudentScoreSubjectDetailService studentScoreSubjectDetailService;
    @Resource
    private StudentScoreSubjectDetailMapper studentScoreSubjectDetailMapper;
    @Autowired
    private StudentScoreTotalDetailService studentScoreTotalDetailService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private GradeTeacherRService gradeTeacherRService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private CompanyService companyService;
    @Override
    public IPage<StudentScoreSubjectDetailVO> getPageList(StudentScoreSubjectDetailVO studentScoreSubjectDetailVO){
       Page<StudentScoreSubjectDetail> page = new Page<>();
       page.setCurrent(studentScoreSubjectDetailVO.getCurrentPage());
       page.setSize(studentScoreSubjectDetailVO.getPageSize());
       IPage<StudentScoreSubjectDetailVO> pageList = studentScoreSubjectDetailMapper.getPageList(page, studentScoreSubjectDetailVO);
       List<StudentScoreSubjectDetailVO> list = pageList.getRecords();
       pageList.setRecords(list);
       return pageList;
}
    @Override
    public List<StudentScoreSubjectDetail> getAllList(StudentScoreSubjectDetailVO studentScoreSubjectDetailVO){
        QueryWrapper<StudentScoreSubjectDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<StudentScoreSubjectDetail> list = studentScoreSubjectDetailService.list(queryWrapper);
        return list;
}
    @Override
    public String add(StudentScoreSubjectDetailVO studentScoreSubjectDetailVO){
        StudentScoreSubjectDetail entity = new StudentScoreSubjectDetail();
        BeanUtils.copyProperties(studentScoreSubjectDetailVO, entity, SysConf.STATUS);
        entity.insert();
        studentScoreSubjectDetailVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }
    @Override
    public String edit(StudentScoreSubjectDetailVO studentScoreSubjectDetailVO){
        StudentScoreSubjectDetail entity = studentScoreSubjectDetailService.getById(studentScoreSubjectDetailVO.getUid());
        BeanUtils.copyProperties(studentScoreSubjectDetailVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
    @Override
    public String delete(StudentScoreSubjectDetailVO studentScoreSubjectDetailVO){
        StudentScoreSubjectDetail entity = studentScoreSubjectDetailService.getById(studentScoreSubjectDetailVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}

    @Override
    public StudentScoreSubjectDetailVO singleImport(StudentScoreSubjectDetailVO studentScoreSubjectDetailVO, StudentScoreBath studentScoreBath) {
        String companyCode = null;
        Dept suerDept = deptService.getDeptByName(studentScoreSubjectDetailVO.getSuperDeptName());
        if(suerDept == null){
            throw new RuntimeException("上级机构不存在");
        }
        if(StringUtils.isNotBlank(studentScoreBath.getDeptUid())){
            Dept dept = deptService.getById(studentScoreBath.getDeptUid());
            if(dept != null){
                if(!studentScoreSubjectDetailVO.getDeptName().equals(dept.getName())){
                    throw new RuntimeException("该机构不符合");
                }
                companyCode = dept.getCompanyCode();
            }else {
                Company company = companyService.getById(studentScoreBath.getDeptUid());
                if(company == null){
                    throw new RuntimeException("该机构不存在");
                }
                if(!studentScoreSubjectDetailVO.getDeptName().equals(company.getShortName())){
                    throw new RuntimeException("该机构不符合");
                }
                companyCode = company.getCompanyCode();
            }
        }
        String name = "";
        if(StringUtils.isNotBlank(studentScoreSubjectDetailVO.getStudentName())){
            name = studentScoreSubjectDetailVO.getStudentName();
        }
        if(StringUtils.isNotBlank(studentScoreSubjectDetailVO.getEmployeeName())){
            name = studentScoreSubjectDetailVO.getEmployeeName();
        }
        String suffix = null;
        if(name.contains("(")){
            suffix = name.split("\\(")[1].substring(0,name.split("\\(")[1].length()-1);
            name = name.split("\\(")[0];
        }
        EmployeeVO employeeVO = new EmployeeVO();
        if(StringUtils.isNotBlank(name)){
            employeeVO.setName(name);
        }
        if(StringUtils.isNotBlank(name)){
            employeeVO.setNameEndWith(suffix);
        }
        if(StringUtils.isNotBlank(studentScoreSubjectDetailVO.getEmployeeCode())){
            employeeVO.setEmployeeCode(studentScoreSubjectDetailVO.getEmployeeCode());
        }
        employeeVO = employeeService.getEmployeeMesByCodeAndNameOrCertCodeAndSuffix(employeeVO);
        if(employeeVO == null){
            throw new RuntimeException("该学生不存在,或者已毕业");
        }

        if(StringUtils.isNotBlank(name) && !name.equals(employeeVO.getName())){
            throw new RuntimeException("该学生不存在");
        }
        studentScoreSubjectDetailVO.setStudentUid(employeeVO.getUid());
        Grade grade;
        if(StringUtils.isNotBlank(studentScoreSubjectDetailVO.getDeptName())){
            grade = gradeService.getGradeByName(companyCode,studentScoreSubjectDetailVO.getDeptName());
        }else {
            grade = gradeService.getGradeByDeptCode(employeeVO.getCompanyCode());
        }
        if(grade == null){
            throw new RuntimeException("该班级不存在");
        }
        studentScoreSubjectDetailVO.setDeptUid(grade.getUid());
        GradeTeacherR teacher = gradeTeacherRService.getTeacherBySubjectTypeAndGrade(grade.getUid(), studentScoreSubjectDetailVO.getSubjectType());
        if(teacher == null || studentScoreSubjectDetailVO.getSubjectType() == 0){
            throw new RuntimeException("该科目不存在教师");
        }
        studentScoreSubjectDetailVO.setTeacherUid(teacher.getUid());
        studentScoreSubjectDetailService.add(studentScoreSubjectDetailVO);
        return studentScoreSubjectDetailVO;
    }

    @Override
    public String generateRank(StudentScoreSubjectDetailVO studentScoreSubjectDetailVO) {
        List<StudentScoreSubjectDetail> list = studentScoreSubjectDetailMapper.generateRank(studentScoreSubjectDetailVO);
        for (StudentScoreSubjectDetail studentScoreSubjectDetail : list) {
            studentScoreSubjectDetail.updateById();
        }
        return ResultUtil.successWithMessage("重新生成排名成功");
    }

    @Override
    public List<StudentScoreSubjectDetailVO> getStudentScoreInfoList(StudentScoreSubjectDetailVO studentScoreSubjectDetailVO,String dictType) {
        return studentScoreSubjectDetailMapper.getStudentScoreInfoList(studentScoreSubjectDetailVO,dictType);
    }
}
